package com.cloudptt.api.product.func.addr;

import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.message.NotifyNewGroupListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;
import com.mcptt.http.operator.IHttpAddrOpCb;

import android.content.Context;

public class AddrFunc  implements IFunc,IHttpAddrOpCb,CallBack
{
	public final static String Log_Key = "AddrFunc";
	private AddrBroadcast broadcast = null;
	private SysConfig config = null;
	private AddrOperator op = null;
	private AddrListener listener = null;
	private boolean isFirstLogined = true;

	public AddrFunc(Context ctx, SysConfig config, AddrOperator op)
	{
		this.broadcast = new AddrBroadcast(ctx,this);
		this.config = config;
		this.op = op;
	}

	public void setListener(AddrListener listener) {
		this.listener = listener;
	}

	public void setNewGroupListener(NotifyNewGroupListener newGroupListener) {
		this.broadcast.setNewGroupListener(newGroupListener);
	}

	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onLogined info = " + info + " isFirstLogined = " + isFirstLogined);
		if(isFirstLogined)
		{
			loadAddress();
			isFirstLogined = false;
		}
	}
	@Override
	public void onPublishResult(int publishResult)
	{}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		isFirstLogined = true;
	}
	@Override
	public void doLogout() {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "doLogout");
		isFirstLogined = true;
	}
	@Override
	public void doExit()
	{
		this.broadcast.clearResource();
		this.broadcast = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "doLogout isconnected = " + isconnected);
//		if(Product.getProductType() != ProductConst.ProductType_NAPP_RADIO)
//		{
//			if(!isconnected)
//			{
//				dbOperator.cleanAllLogin();
//			}
//			else
//			{
//				if(info != null)
//				{
//					String svrIp = info.getServerIp();
//					op.loadOnlineState(svrIp, this);
//				}
//			}
//		}
	}



	@Override
	public void onAddrUptResult(boolean isSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onAddrUptResult isSuc = " + isSuc);
		if(isSuc)
		{
			//			List<GroupInfo> groupList = dbOperator.getGroupList();
			//			String defaultGroupNum = defaultOp.getDefaultGroupInfo();
			if(listener != null)
			{
				listener.onAddrUpt();
			}

		}
	}



	@Override
	public void onOnlineUptResult(boolean isSuc) {
		// TODO Auto-generated method stub
		Log.d(Log_Key, "onOnlineUptResult isSuc = " + isSuc);
		if(isSuc
				&& listener != null)
		{
			listener.onOnlineUpt();
		}
	}

	@Override
	public void loadAddress() {
		// TODO Auto-generated method stub
		if(config != null)
		{
			op.loadAddress(config, this);
		}
	}
}
